﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace RPGXEngine
{
    public class Command_If : ICommand
    {
        #region Classes

        public class IfPart
        {
            public ICommand Condition = null;
            public List<ICommand> Commands = new List<ICommand>();
        }

        #endregion

        #region ICommand Members

        public List<IfPart> IfParts
        {
            get { return _ifParts; }
            set { _ifParts = value; }
        }
        private List<IfPart> _ifParts = new List<IfPart>();

        public bool IsReturn { get { return false; } }
        public bool IsIf { get { return true; } }

        public bool IsElse
        {
            get { return _isElse; }
            set { _isElse = value; }
        }
        private bool _isElse = false;

        public List<ICommand> ChildrenCommands
        {
            get
            {
                return this._childCommands;
            }
        }
        private List<ICommand> _childCommands = new List<ICommand>();

        public IVariableType Execute(ScriptManager sm, FunctionScope scope)
        {
            foreach (IfPart part in this.IfParts)
            {
                IVariableType res = sm.True;

                if (part.Condition != null)
                {
                    res = part.Condition.Execute(sm, scope);
                }

                if ((bool)res.Value == (bool)sm.True.Value)
                {
                    foreach (var cmd in part.Commands)
                    {
                        cmd.Execute(sm, scope);
                    }

                    break;
                }
            }

            return new VoidVariableType(String.Empty);
        }

        #endregion
    }
}
